package com.dqhl.business.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.business.Base.BaseActivity;
import com.dqhl.business.R;
import com.dqhl.business.modle.CouponDetail;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * description: 优惠券详情
 * autour: guanluocang
 * date: 2016/11/8 15:32
 */
public class CouponDetailActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_goodsTopTitle;
    private ImageView iv_goodsGoBack;
    private CouponDetail couponDetail;
    private TextView tv_type;
    private TextView tv_message;
    private TextView tv_time;
    private TextView tv_limited;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_detail);
        initView();
        initData();
    }

    private void initView() {
        tv_goodsTopTitle = (TextView) findViewById(R.id.tv_goodsTopTitle);
        tv_goodsTopTitle.setText("优惠券详情");
        iv_goodsGoBack = (ImageView) findViewById(R.id.iv_goodsGoBack);
        iv_goodsGoBack.setOnClickListener(this);

        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_message = (TextView) findViewById(R.id.tv_message);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_limited = (TextView) findViewById(R.id.tv_limited);
    }

    private void initData() {
        String coupon_id = getIntent().getStringExtra("coupon_id");
        Dlog.e(coupon_id);
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.personal_coupon_content);
        params.addBodyParameter("coupon_id", coupon_id);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getData(result);
                if (0 == errCode) {
                    couponDetail = JSON.parseObject(data, CouponDetail.class);

                    tv_type.setText(couponDetail.getType());
                    tv_message.setText(couponDetail.getMessage());
                    tv_time.setText(couponDetail.getTime());
                    tv_limited.setText(couponDetail.getLimited() + "张");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_goodsGoBack:
                finish();
                break;
        }
    }
}
